const {
  toUrl,
  globalData
} = getApp();
import {
  request
} from "@/utils/request";
Page({
  toUrl,
  data: {
    globalData,
    menu: 0,
    currentIndex: 0,
    couponList: [],
    form: {
      typeid: 1,
      type: 0, //0-全部 1-平台通用 2-店铺专享 3-无门槛 4-满减
      page: 1,
    },
    count: 1,
    isExit: false,
  },
  getList() {
    const that = this;
    const data = this.data.form;
    data.type = this.data.menu;
    if (this.data.form.page > this.data.count) {
      this.setData({
        isExit: true,
      });
      return;
    }
    request("coupon/couponlist", {
      data
    }).then((res) => {
      const form = this.data.form;
      form.page = ++form.page;
      that.setData({
        couponList: that.data.couponList.concat(res.data),
        form: form,
        count: res.count,
      });
    });
  },
  //领取优惠券
  getTicket(e) {
    const coupon = e.currentTarget.dataset.coupon; //优惠券id
    const index = e.currentTarget.dataset.index;
    const receive_type = e.currentTarget.dataset.receive_type
    const integral = e.currentTarget.dataset.integral
    const money = e.currentTarget.dataset.money
    let totalintegral = getApp().globalData.baseInfo.integral
    let status = e.currentTarget.dataset.status
    const that = this;
    const data = {
      coupon_id: coupon,
    };
    if (receive_type == 2) {
      if (status == 2) {
        wx.showToast({
          title: `已兑换`,
          icon: "none"
        });
        return
      }
      if (totalintegral < integral) {
        wx.showToast({
          title: `当前积分不足 \r\n 无法兑换`,
          icon: "none"
        });

        return
      }
      wx.showModal({
        content: `是否用${integral}积分兑换`,
        confirmColor: "#1D2A73",
        cancelColor: "#666",
        confirmText: "确定",
        success(res) {
          if (res.confirm) {
            request("coupon/apply", {
              data
            }).then((res) => {
              if (res.errcode == 1) {
                const list = that.data.couponList;
                list[index].status = 2;
                wx.showToast({
                  title: "兑换成功",
                });
                that.setData({
                  couponList: list,
                });
              }
            });
          } else if (res.cancel) {
            console.log("用户点击取消");
            return
          }
        },
      });
      return
    }
    request("coupon/apply", {
      data
    }).then((res) => {
      if (res.errcode == 1) {
        const list = this.data.couponList;
        list[index].status = 2;
        wx.showToast({
          title: "领取成功",
        });
        that.setData({
          couponList: list,
        });
      }
    });
  },
  swichTab(e) {
    const index = e.currentTarget.dataset.index;
    if (index == this.data.currentIndex) {
      return;
    }
    const form = this.data.form;
    form.type = index;
    form.page = 1;
    this.setData({
      menu: index,
      form: form,
      count: 1,
      isExit: false,
      currentIndex: index,
      couponList: [],
    });
    this.getList();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getList();
  },

  onReachBottom() {
    if (this.data.isExit) {
      return;
    } else {
      this.getList();
    }
  },
});